﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 基于窗口的WinForm
{
    public partial class Form1 : Form
    {
        private int x, y;
        private Bitmap bmall;//原始位图
        private Bitmap[] bmfragments = new Bitmap[48];
        private int displayindex = 0;//起始帧索引
        private int timetickcount;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat(
             "窗口大小（像素）：{0}{1}",
             this.Width.ToString(),
             this.Height.ToString());
            sb.AppendLine();
            sb.AppendFormat(
                "窗口大小（像素）：{0}{1}",
                this.ClientRectangle.Width.ToString(),
                this.ClientRectangle.Height.ToString());
            MessageBox.Show(sb.ToString(), "信息提示");
            //初始化计时器
            this.toolStripStatusLabel1.Text = "计时次数：" + timetickcount.ToString();
            this.timer1.Interval = 1000;
            this.timer1.Start();
            bmall = new Bitmap(Application.StartupPath + "\\threerings.bmp");
            //一位数组的处理方法
            for (int row = 0; row < 8; row++)
            {
                for (int col = 0; col < 6; col++)
                {
                    bmfragments[row * 6 + col] = bmall.Clone(
                        new Rectangle(col*75,row*75,75,75),bmall.PixelFormat);
                }
                this.timer2.Interval = 80;
                this.timer2.Start();
            }
        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            Debug.WriteLine(e.KeyCode.ToString());
            //按Esc键关闭窗口
            if (e.KeyCode == Keys.Escape)
            {
                this.Close();
            }
        }

        private void Form1_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                MessageBox.Show("单击左键", "提示");
            }
            else if (e.Button == MouseButtons.Right)
            {
                MessageBox.Show("单击右键", "提示");
            }
            else if (e.Button == MouseButtons.Middle)
            {
                MessageBox.Show("单击中键", "提示");
            }
            
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            this.x = e.X;
            this.y = e.Y;
            this.Invalidate();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            timetickcount++;
            this.toolStripStatusLabel1.Text = "计时次数：" + timetickcount.ToString();
        }

        private void timer2_Tick(object sender, EventArgs e)
        {
            //动画逻辑
            displayindex = (++displayindex) % 48;
            if (displayindex == 47 && this.timer2.Interval >= 28)
            {
                this.timer2.Interval -= 10;
            }
            //刷新界面
            this.Invalidate();

        }
        protected override void OnPaint(PaintEventArgs e)
        {
            //预先在内存中绘制所有内容,再一次性绘制到前端
            Bitmap buff = new Bitmap(this.ClientRectangle.Width,
                this.ClientRectangle.Height);
            Graphics g = Graphics.FromImage(buff);
            //绘制到缓冲区（内存）
            g.DrawImage(bmfragments[displayindex], new Point(500, 350));
            //绘制到前端
            e.Graphics.DrawImage(buff, 0, 0);
            //释放内存
            g.Dispose(); 


            base.OnPaint(e);
        }
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            //图形工具类
            Graphics g = e.Graphics;
            //绘制鼠标坐标信息--文字
            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("鼠标当前坐标：{0},{1}", x.ToString(), y.ToString());
            e.Graphics.DrawString(sb.ToString(),
                new Font("微软雅黑", 20f),
                new SolidBrush(Color.Crimson),
                350f, 280f);
            //绘制分割线
            Pen pen = new Pen(Color.Black,2.0f);
            g.DrawLine(pen, this.ClientRectangle.Width / 2,
                0,
                this.ClientRectangle.Width / 2,
                this.ClientRectangle.Height 
                );
            g.DrawLine(pen, 0,this.ClientRectangle.Height / 2,
                this.ClientRectangle.Width, 
                this.ClientRectangle.Height/2
                );
            //绘制图形
            pen.Color = Color.Blue;
            g.DrawRectangle(pen, 2, 2, 300, 300);
            g.FillRectangle(new SolidBrush(Color.LightBlue), 15, 15, 274, 214);
            pen.Color = Color.DeepPink;
            g.DrawBezier(pen,
                new Point(1, 1),
                new Point(20, 20),
                new Point(52, 32),
                new Point(120, 100)
                );
            //绘制文字
            g.DrawString("游戏开始",new Font("微软雅黑",32),new SolidBrush(Color.OrangeRed),
                new Point(400,40));
            //绘制图片
            Bitmap bm = new Bitmap(Application.StartupPath + "\\c.png");
            g.DrawImage(bm, new Rectangle(2, 290, 360, 240));



        }
    }
}
